<?php

namespace app\admin\controller;

use app\common\model\Banner;
use think\Request;
use app\common\model\AuthRule;
use app\common\model\SiteSetting;

class SettingController extends BaseController
{

    /**
     * 网站配置
     * @params string
     * @return
     */
    public function site(Request $request)
    {
        $data = $request->post();
        if($this->method == 'POST')
        {
            $tab = $request->post('tab');
            if($tab == 'tab1')
            {
                SiteSetting::editSite($data);
            }else if($tab == 'tab2'){
                if(empty($data['path_info']['local']['img_path']) || empty($data['path_info']['local']['other_path']))
                {
                    return returnFail('本地存储路径不能为空');
                }
                SiteSetting::editUploadSetting($data);
            }
            return returnSuccess('修改成功');
        }else{
            //网站配置
            $site_info = SiteSetting::getSite();
            //上传配置
            $upload = SiteSetting::getUploadSetting();
            //print_r($upload);

            return view('setting/site', ['info'=>$site_info, 'upload'=>$upload]);
        }
    }

    /**
     * banner
     * @params string
     * @return
     */
    public function banner()
    {
        if($this->method == 'POST')
        {
            $data = Banner::getBanner();

            return returnData($data['list'], $data['total']);
        }else{
            return view('setting/banner_list');
        }
    }

    /**
     * 添加banner
     * @params string
     * @return
     */
    public function addBanner(Request $request)
    {
        if($this->method == 'POST')
        {
            $data = $request->post();
            $res = Banner::create($data);
            if($res)
            {
                return returnSuccess('添加成功');
            }
            return returnFail('添加失败');
        }else{
            return view('setting/add_banner');
        }
    }

    /**
     * 添加banner
     * @params string
     * @return
     */
    public function editBanner(Request $request)
    {
        if($this->method == 'POST')
        {
            $data = $request->post();
            $res = Banner::update($data, ['id'=>$data['id']]);
            if($res)
            {
                return returnSuccess('修改成功');
            }
            return returnFail('修改失败');
        }else{
            $assign['info'] = Banner::find($request->get('id'));
            return view('setting/edit_banner', $assign);
        }
    }

    /**
     * 删除banner
     * @params string
     * @return
     */
    public function delBanner(Request $request)
    {
        if($this->method == 'POST')
        {
            $id = $request->post('id');

            Banner::where('id', $id)->delete();

            return returnSuccess('删除成功');
        }
        return returnFail('非法请求');
    }
}
